/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ilookinto.jr;

import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;


public class URLUtils {

  public static void main(String s[]) {
    System.out.println(URLUtils.exists("http://www.rgagnon.com/howto.html"));
    System.out.println(URLUtils.exists("http://www.rgagnon.com/pagenotfound.html"));
    /*
      output :
        true
        false
    */
  }
  static String auth;
    static {



        String name = "admin";
        String password = "admin";

        String authString = name + ":" + password;
        System.out.println("auth string: " + authString);
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        auth = new String(authEncBytes);
        //System.out.println("Base64 encoded auth string: " + authStringEnc);
    }
  public static boolean exists(String URLName){
    try {
      HttpURLConnection.setFollowRedirects(false);
      // note : you may also need
      //        HttpURLConnection.setInstanceFollowRedirects(false)
      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      con.setRequestProperty("Authorization", "Basic " + auth);
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }
}

